package top.api.utils;

import cn.hutool.captcha.AbstractCaptcha;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.util.Base64Utils;

import javax.imageio.ImageIO;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.*;

import java.util.HashMap;

import java.util.Map;

import java.util.Random;

/**
 * 生成base64图形验证码
 */
public class Base64CodeUtils {

    public static void main(String[] args) {

        Map captchaPic = getCodeMap();
        System.out.println(captchaPic.get("base64Str"));
        System.out.println(captchaPic);

    }

    /**
     * 数字运算 8*7=
     * @Author yql
     * @Date 2023-07-21 22:42
     */
    public static Map<String,String > getMathCodeMap(){
        AbstractCaptcha captcha = new CircleCaptcha(200, 45,14,15);
        captcha.setFont(new Font("Arial", Font.BOLD,48));
        captcha.setBackground(new Color(255,175,175));
        // 自定义验证码内容为四则运算方式
        captcha.setGenerator(new MathGenerator(1));
        // 重新生成code
        captcha.createCode();
        String code = captcha.getCode();
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(StringUtils.remove(code, "="));
        code = exp.getValue(String.class);

        Map<String, String> map = new HashMap<>();
        map.put("code",code);
        map.put("base64Str","data:image/jpeg;base64,"  + captcha.getImageBase64());

        return map;
    }

    /**
     * 生成base64 图像验证码
     * key: base64Str,code
     * @return
     */
    public static Map getCodeMap() {

        int width = 160;

        int height = 60;

        int lines = 10;

        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Graphics g = img.getGraphics();

// 设置背景色

        g.setColor(Color.WHITE);

        g.fillRect(0, 0, width, height);

// 设置字体

        g.setFont(new Font("cmr10", Font.BOLD, (int) (height / 1.2)));

// 随机数字

        Random r = new Random(System.currentTimeMillis());

        String code = "";

        for (int i = 0; i < 4; i++) {

            int a = r.nextInt(10);

            Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));

            g.setColor(c);

            g.drawString("" + a, 5 + i * width / 4, (int) (height / 1.3) + r.nextInt(height / 8));

            code = code + a;

        }

// 干扰线

        for (int i = 0; i < lines; i++) {

            Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));

            g.setColor(c);

            g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));

        }

        g.dispose();// 类似于流中的close()带动flush()---把数据刷到img对象当中

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        Map map = new HashMap<>(2);

        try {

            ImageIO.write(img, "jpg", outputStream);

            byte[] base64Img = Base64Utils.encode(outputStream.toByteArray());

            map.put("base64Str", "data:image/jpeg;base64," + new String(base64Img).replaceAll("\n", ""));

            map.put("code", code);

            return map;

        } catch (IOException e) {

            e.printStackTrace();

            return null;

        }

    }

}
